﻿using FBroSharp;
using FBroSharp.Callback;
using FBroSharp.Const;
using FBroSharp.DataType;
using FBroSharp.Event;
using FBroSharp.Lib;
using FBroSharp.Value;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace URL请求测试_带浏览器环境
{
    public class Event : FBroInitEvent
    {
        //即将处理命令行
        public override void OnBeforeCommandLineProcessing(string process_type, IFBroSharpCommandLine cmd)
        {

            ////关闭GPU加速
            cmd.DisableGpuBlockList();
            cmd.DisableGpuCache();

            Console.WriteLine(MethodBase.GetCurrentMethod().Name + ":" + process_type + cmd.IsReadOnly() + cmd.GetProgram());
        }

        //初始化完毕
        public override void OnContextInitialized()
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name);
 
        }

        public override void OnBeforeChildProcessLaunch(IFBroSharpCommandLine command_line)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name + ":" + command_line.IsReadOnly() + command_line.GetProgram());
        }

    }


    public class BrowserEvent : FBroBrowserEvent
    {
        public static FBroSharpBrowser mainbrowser_ = new FBroSharpBrowser();

        public  override void OnAfterCreated(IFBroSharpBrowser browser, IFBroSharpDictionaryValue extrainfo)
        {
            mainbrowser_.CopyFrom(browser);
        }

        public override bool OnBeforePopup(IFBroSharpBrowser browser, IFBroSharpFrame frame, string target_url, string target_frame_name,
            FBroSharpWindowOpenDisposition target_disposition, bool user_gesture, FBroSharpPopupfeatures popupFeatures, 
            ref FBroSharpWindowsInfo windowInfo, ref FBroSharpBrowserSetting settings, ref bool no_javascript_access, IFBroSharpUseExtraData user_settings)
        {
            browser.GetMainFrame().LoadURL(target_url);
            return true;
        }

    }

    public class URLEvent : FBroSharpURLEvent
    {
        //用于存放全部数据
        private MemoryStream responseStream_;

        private BinaryWriter writer_;

        public override void Start(long flag, IFBroSharpURLRequest request)
        {
            responseStream_ = new MemoryStream();
            writer_ = new BinaryWriter(responseStream_);

            Console.WriteLine("Name:{0} Flag:{1} Request url:{2} Referrer url:{3}", MethodBase.GetCurrentMethod().Name, flag, request.GetRequestStatus(), request.GetRequest().GetURL(), request.GetRequest().GetReferrerURL());
        }

        public override void End(long flag)
        {
            Console.WriteLine("Name:{0} Flag:{1}", MethodBase.GetCurrentMethod().Name, flag);
           
            writer_ = null;
            responseStream_ = null;
           
        }

        public override bool GetAuthCredentials(long flag, bool isProxy, string host, int port, string realm, string scheme, IFBroSharpAuthCallback callback)
        {
            Console.WriteLine("Name:{0} Flag:{1}", MethodBase.GetCurrentMethod().Name, flag);
            return false;
        }

        public override void OnDownloadData(long flag, IFBroSharpURLRequest request, Stream data)
        {
            Console.WriteLine("Name:{0} Flag:{1} RequestStatus:{2}", MethodBase.GetCurrentMethod().Name, flag, request.GetRequestStatus());

            //如果存在多数据这个会多次调用本事件，因此数据先进行暂存到writer_中
            var readBytes = new byte[data.Length];
            data.Read(readBytes, 0, readBytes.Length);
            writer_.Write(readBytes);
        }

        public override void OnDownloadProgress(long flag, IFBroSharpURLRequest request, long current, long total)
        {
            Console.WriteLine("Name:{0} Flag:{1} RequestStatus:{2}", MethodBase.GetCurrentMethod().Name, flag, request.GetRequestStatus());
        }

        public override void OnRequestComplete(long flag, IFBroSharpURLRequest request)
        {
            Console.WriteLine("Name:{0} Flag:{1} RequestStatus:{2}", MethodBase.GetCurrentMethod().Name, flag, request.GetRequestStatus());

            //执行完毕再显示数据
            Console.WriteLine(Encoding.UTF8.GetString(responseStream_.ToArray()));

        }

        public override void OnUploadProgress(long flag, IFBroSharpURLRequest request, long current, long total)
        {
            Console.WriteLine("Name:{0} Flag:{1} RequestStatus:{2}", MethodBase.GetCurrentMethod().Name, flag, request.GetRequestStatus());
        }


    }
}